const snapModel = require('../model/snapModel');


// 查询
module.exports.find = async function (searchObj, page) {
    let { current, pageSize } = page;
    // 根据查询条件获取总数
    let total = await snapModel.countDocuments(searchObj);
    // 根据查询条件获取具体数量的数据
    let data = await snapModel.find(searchObj)
        .limit(pageSize).skip((current - 1) * pageSize);
    return { current, pageSize, total, rows: data }
}
// 查所有
module.exports.findAllSnap = async function () {
    return await snapModel.find();
}

// 增加
module.exports.create = async function (snap) {
    return await snapModel.create(snap);
}

// 删
module.exports.remove = async function (id) {
    return await snapModel.remove({ _id: id });
}

// // 改通过id得数据
// module.exports.findById = async function (id) {

//     console.log("dao:", await snapModel.findById({ _id: id }));
//     return await snapModel.findById({ _id: id });
// }
// 改通过id得数据
module.exports.findById = async function (id) {
    console.log("dao:", await snapModel.findById({ _id: id }));
    return await snapModel.findById({ _id: id });
}

// 修改
module.exports.update = async function (id, snap) {
    return await snapModel.updateOne({ _id: id }, snap);
}


// 查order id
module.exports.findByOrderId = async function (orderId) {
    console.log("得orderid:", await snapModel.find({ order: orderId }).populate({ path: 'order' }));
    return await snapModel.find({ order: orderId }).populate({ path: 'order' });
}